package modelLayer;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Dvd Container is a class that holds information about DVD objects.
 * 
 * @author group 6 - Stefan Patatu, Stan Cosmin Madalin, Monika Vysniauskaite, Larowski Michal, Njogu Alex, Cristian Moldovan
 */
public class DVDContainer {
   public ArrayList<DVD> dvds;
   private static DVDContainer instance = null;
   
   /**
    * Create an arraylist of type DVD
    */
   private DVDContainer(){
       dvds = new ArrayList<>();
   }
   
   /**
     * Singleton pattern which ensure that only one instance of a class is created.
     * @param instance The instance of the DVDContainer class.
     */
   public static DVDContainer getInstance() {
        if (instance == null) {
            instance = new DVDContainer();
        }
        return instance;
   }
   
   /**
    * Add a given DVD object to dvds ArrayList
    * param dvd The DVD object to be added
    */
   public void addDVD(DVD dvd) {
       dvds.add(dvd);
   }
   
   /**
    * Get a DVD object by a given barcode.
    * @param barcode The barcode of a DVD object
    */
   public DVD getDVD(long barcode) {
       for(DVD dvd : dvds){
           if(dvd.getBarcode() == barcode) {
               return dvd;
           }
       }
       return null;
   }
   
   public DVD findDVD(long barcode) {
       for(DVD dvd : dvds) {
           if(dvd.getBarcode() == barcode) {
               return dvd;
            }
        }
       return null;
   }
   
   public void updateDVD(long barcode, String title, String artist, String publicationDate) {
       for(DVD dvd : dvds) {
           if(dvd.getBarcode() == barcode){
               dvd.setTitle(title);
               dvd.setArtist(artist);
               dvd.setPublicationDate(publicationDate);
           }
       }
   }
   
   /**
    * Delete a DVD object which by a given barcode.
    * @param barcode The barcode of the DVD object to be deleted.
    */
   public void deleteDVD(long barcode) {
       for(Iterator<DVD> i = dvds.iterator(); i.hasNext();) {
           DVD dvd = i.next();
           if(dvd.getBarcode() == barcode){
               i.remove();
           }
       }
   }
   
   /**
    * Find o copy of a DVD by its given serial number.
    * @param serialNumber The serial number of the searched copy
    */
   public Copy findCopy(int serialNumber) {
       Copy returnedCopy;
       for(DVD dvd : dvds){
           returnedCopy = dvd.findCopy(serialNumber);
           if(returnedCopy != null) {
               return returnedCopy;
            }
       }
       return null;
   }
   
   public int getNumberOfDVDCopies(int barcode) {
       for(Iterator<DVD> i = dvds.iterator(); i.hasNext();) {
           DVD dvd = i.next();
           if(dvd.getBarcode() == barcode){
               return dvd.getNumberOfCopies();
           }
       }
       return 0;
   }
}
